package com.wangbo.restTemplate.aiSchool;

import com.alibaba.fastjson.JSONObject;
import com.wangbo.restTemplate.aiSchool.pojo.PersonDetailDTO;
import com.wangbo.restTemplate.aiSchool.pojo.ResultObj;
import com.wangbo.restTemplate.aiSchool.pojo.StudentAdd;
import com.wangbo.restTemplate.aiSchool.pojo.UserRegisterParam;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

public class TestAISchool {
    public static final RestTemplate restTemplate = new RestTemplateBuilder()
            .setConnectTimeout(Duration.ofSeconds(3L))
            .setReadTimeout(Duration.ofSeconds(3L))
            .errorHandler(new DefaultResponseErrorHandler(){
        /**
         * 设置RestTemplate自带的异常处理器不做异常处理
         **/
        @Override
        public void handleError(ClientHttpResponse response) {
        }
    }).build();

    public static void main(String[] args) {

//        File dir = new File("D:\\download\\实小新1年级(1)\\一6班");
//        File[] files = dir.listFiles();
//        for (File file : files) {
//            String fileName = file.getName();
//            String num = "202106" + fileName.split("_")[0];
//            String name = fileName.split("_")[1].split("\\.")[0];
//
//            System.out.println(num + name);
//            System.out.println(file.getAbsolutePath());
//
//            Long mid = test02(file.getAbsolutePath());
//            System.out.println(mid);
//            test04(mid, "xxoo", "19912345678", name, num, 51);
//        }

        // feature 远端识别
        test05();
    }

    /**
     * 获取验证码
     */
    public static void test01(String phone) {
        String url = "http://120.92.137.180:8080/aiSchool/smsCode/" + phone;
        String result = restTemplate.getForObject(url, String.class);
        System.out.println(result);
    }

    /**
     * 上传人脸
     */
    public static Long test02(String path) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("multipartFile ", new FileSystemResource(path));

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
        ResultObj result = restTemplate.postForObject("http://120.92.141.239:8080/aiSchool/sys/media", httpEntity, ResultObj.class);
        System.out.println(result);
        return (long)(int) result.getData();
    }

    /**
     * 注册用户
     */
    public static void test03(long mid, String captcha, String phone,String name, String personalIdNo) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String url = "http://120.92.137.180:8080/aiSchool/registerUser";

        UserRegisterParam userRegisterParam = new UserRegisterParam()
                .setMid(mid)
                .setUnitid(1L)
                .setCaptcha(captcha)
                .setUsername(phone)
                .setName(name)
                .setPersonalIdNo(personalIdNo);

        HttpEntity<UserRegisterParam> httpEntity = new HttpEntity<>(userRegisterParam, headers);

        String result = restTemplate.postForObject(url, httpEntity, String.class);
        System.out.println(result);

    }

    /**
     * 添加学生
     */
    public static void test04(long mid, String parentName, String parentPhone, String name, String personalIdNo, long gid) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String url = "http://120.92.141.239:8080/aiSchool/biz/person-student";

        StudentAdd studentAdd = new StudentAdd()
                .setUnitid(2L)
                .setName(name)
                .setMid(mid)
                .setGid(gid)
                .setParentName(parentName)
                .setParentPhone(parentPhone)
                .setPersonalIdNo(personalIdNo);

        HttpEntity<StudentAdd> httpEntity = new HttpEntity<>(studentAdd, headers);

        String result = restTemplate.postForObject(url, httpEntity, String.class);
        System.out.println(result);
    }

    /**
     * feature 识别
     */
    public static void test05() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String url = "http://192.168.88.206:8080/aiSchool/biz/personDetail/featureStr";

        String featureStr = "oecVPrrSQj2jAi49Dpv6vXL2UDxUmqM90QVEPWY4mzy8kxQ9uex1PZVf+Lwm39i8Y3ktvUtA3zyEaHw9rTjDvbpxFT0mXuC6sLyaPU10WD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j3O3Pcgm5zxVcsQ9LVzWPMa+wLwDks87P+J8vTIyCT4bOLq9pFRTvcKW073SVSy9vF1ivKRavDxTywy9OdqIPR5hgb1Qaes7vvvGvI9TFTwfOYM7RLRLvR6IHDxkH/C9SLgcPBhtXbwkAZk9fJ6Hu0OItL3Ovvm8/+0cPQ==";
        HttpEntity<String> httpEntity = new HttpEntity<>(featureStr, headers);

        PersonDetailDTO personDetailDTO = restTemplate.postForObject(url, httpEntity, PersonDetailDTO.class);
        System.out.println(personDetailDTO);
    }
}
